﻿CREATE PROCEDURE [dbo].[Text_Get]
	@userId UNIQUEIDENTIFIER
	, @hashTag NVARCHAR(128)
	, @textId BIGINT
AS
BEGIN
	DECLARE @order INT, @chapterId UNIQUEIDENTIFIER

	SET @hashTag = LOWER(@hashTag)
	BEGIN TRY
		BEGIN TRANSACTION

			SELECT @chapterId = [ChapterId]
			FROM [dbo].[text_Chapter]
			WHERE [HashTag] = @hashTag

			IF @chapterId IS NULL OR [dbo].[IsViewUnpublished](@userId) <> 1
			BEGIN
				ROLLBACK TRANSACTION
				RETURN
			END

			SELECT [TextId]
						,[ChapterId]
						,[Value]
						,[Order]
						,[Tag]
			FROM [dbo].[text_Text]
			WHERE [ChapterId] = @chapterId AND [TextId] = @textId

	
		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		ROLLBACK TRANSACTION

		DECLARE @ErrorMessage NVARCHAR(4000);
		DECLARE @ErrorSeverity INT;
		DECLARE @ErrorState INT;

		SELECT	@ErrorMessage = ERROR_MESSAGE(),
						@ErrorSeverity = ERROR_SEVERITY(),
						@ErrorState = ERROR_STATE();

		RAISERROR (
								@ErrorMessage, -- Message text.
								@ErrorSeverity, -- Severity.
								@ErrorState -- State.
							);

	END CATCH

END